
package boundary;

import entity.AmountRange;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;

/**
 * @version 1.0
 * @date 13/04/2012
 * @author john
 */
public class StatsPane extends GridPane{
    
    private final double minTFWidth=200;
    private final double minCBWidth=200;
    
    private final ObservableList reportTypesList;
    private final ObservableList franchisesList;
    private final ObservableList branchOfficesList;
    private final ObservableList<AmountRange> amountRangesList;
    
    private HBox reportTypeHBox;
    private GridPane filterParamGrid;
    private Label reportTypeL;
    private Label startDateL;
    private Label endDateL;
    private Label franchiseL;
    private Label branchOfficeL;
    private Label amountRangeL;
    private TextField startDateTF;
    private TextField endDateTF;
    private ChoiceBox reportTypeCB;
    private ChoiceBox franchiseCB;
    private ChoiceBox branchOfficeCB;
    private ChoiceBox amountRangeCB;
    private Button seeReportB;

    public StatsPane() {
        setId("stats-pane");
        setAlignment(Pos.CENTER);
        setPadding(new Insets(30, 30, 30, 30));
        setHgap(0);
        setVgap(20);
        reportTypesList=FXCollections.observableArrayList();
        franchisesList=FXCollections.observableArrayList();
        branchOfficesList=FXCollections.observableArrayList();
        amountRangesList=FXCollections.observableArrayList();
        initReportTypeHBox();
        initFilterParamGrid();
        seeReportB=new Button("Ver reporte");
        seeReportB.setOnAction(createReport());
        GridPane.setHalignment(seeReportB, HPos.RIGHT);
        add(reportTypeHBox,0,0);
        add(filterParamGrid,0,1);
        add(seeReportB,0,2);
    }
    
    /**
     * 
     */
    private void fillReportTypesList(){
        String allTransactions="Todas las transacciones";
        String validTransactions="Transacciones válidas";
        String invalidTransactions="Transacciones inválidas";
        reportTypesList.setAll(
            allTransactions,
            validTransactions,
            invalidTransactions
        );
    }
    
    /**
     * 
     */
    private void fillAmountRangesList(){
        amountRangesList.setAll(
            new AmountRange(0, 50000),
            new AmountRange(50000, 100000),
            new AmountRange(100000, 250000),
            new AmountRange(250000, 500000),
            new AmountRange(500000, 1000000),
            new AmountRange(1000000, 5000000),
            new AmountRange(5000000)
        );
    }
    
    /**
     * 
     */
    private void fillFranchisesList(){
        String allFranchises="Todas";
        // Esto es de prueba, debe llamar a un metodo control para recuperar todas las sucursales del almacen
        String franchisesTest1="VISA";
        String franchisesTest2="MasterCard";
        String franchisesTest3="American Express";
        franchisesList.setAll(
            allFranchises,
            new Separator(),
            franchisesTest1,
            franchisesTest2,
            franchisesTest3
        );
    }
    
    /**
     * 
     */
    private void fillBranchOfficesList(){
        String allBranchOffices="Todas";
        // Esto es de prueba, debe llamar a un metodo control para recuperar todas franquicias
        String branchOfficeTest1="Calle 80";
        String branchOfficeTest2="Suba Imperial";
        String branchOfficeTest3="Calle 52";
        String branchOfficeTest4="Autonorte";
        branchOfficesList.setAll(
            allBranchOffices,
            new Separator(),
            branchOfficeTest1,
            branchOfficeTest2,
            branchOfficeTest3,
            branchOfficeTest4
        );
    }
    
    /**
     * 
     */
    private void initReportTypeHBox(){
        reportTypeHBox=new HBox(40);
        reportTypeHBox.setPadding(new Insets(20, 0, 0, 0));
        reportTypeL=new Label("Ver reporte de:");
        fillReportTypesList();
        reportTypeCB=new ChoiceBox<>(reportTypesList);
        reportTypeCB.getSelectionModel().select(reportTypesList.get(0));
        reportTypeHBox.getChildren().setAll(reportTypeL,reportTypeCB);
    }
    
    /**
     * 
     */
    private void initFilterParamGrid(){
        fillFranchisesList();
        fillBranchOfficesList();
        fillAmountRangesList();
        filterParamGrid=new GridPane();
        filterParamGrid.setPadding(new Insets(30));
        filterParamGrid.setHgap(25);
        filterParamGrid.setVgap(10);
        filterParamGrid.setId("filters-pane");
        startDateL=new Label("Fecha inicial:");
        endDateL=new Label("Fecha final:");
        franchiseL=new Label("Franquicia:");
        branchOfficeL=new Label("Sucursal:");
        amountRangeL=new Label("Monto:");
        startDateTF=new TextField("dd/mm/aaaa");
        endDateTF=new TextField("dd/mm/aaaa");
        amountRangeCB=new ChoiceBox(amountRangesList);
        amountRangeCB.getSelectionModel().select(amountRangesList.get(0));
        franchiseCB=new ChoiceBox(franchisesList);
        franchiseCB.getSelectionModel().select(franchisesList.get(0));
        branchOfficeCB=new ChoiceBox(branchOfficesList);
        branchOfficeCB.getSelectionModel().select(branchOfficesList.get(0));
        startDateTF.setMinWidth(minTFWidth);
        endDateTF.setMinWidth(minTFWidth);
        amountRangeCB.setMinWidth(minCBWidth);
        franchiseCB.setMinWidth(minCBWidth);
        branchOfficeCB.setMinWidth(minCBWidth);
        filterParamGrid.add(startDateL,0,0);
        filterParamGrid.add(endDateL,0,1);
        filterParamGrid.add(branchOfficeL,0,2);
        filterParamGrid.add(startDateTF,1,0);
        filterParamGrid.add(endDateTF,1,1);
        filterParamGrid.add(branchOfficeCB,1,2);
        filterParamGrid.add(franchiseL,2,0);
        filterParamGrid.add(amountRangeL,2,2);
        filterParamGrid.add(franchiseCB,3,0);
        filterParamGrid.add(amountRangeCB,3,2);
    }
    
    private EventHandler<ActionEvent> createReport(){
        return new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                // Crear y mostrar reporte
                System.err.println("createReport EventHandler not implemented");
            }
        };
    }
    
}